// pages/component/homes/purchase/buycard/buycard.js
var that;
var dataManager = require('../../../../../utils/dataManager.js');
var GlobalData = require('../../../../../utils/GlobalData.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    kindlist: [],
    prolist: [],
    shopcard: GlobalData.shopcar_2,
    procount: 0,
    price: 0,
    isClick: false,
    pic: "Shopping1",
    ishide: true,
    zhekou: "",
    bind: "onClickShopCardup",
    height: "gouwuche",
    hidden: true
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    dataManager.removeBtnObserver("club")
    this.setData({
      hidden: true
    })
    // console.log("生命周期函数--监听页面显示");
    dataManager.rigisterObserver("kindtlist", this.onKindComplete, this);
    dataManager.rigisterObserver("productlist", this.onProComplete, this);
    dataManager.getKindList();

    this.data.price = this.getPrice();
    //计算价格
    this.setData({
      price: this.data.price
    })

    //样式改变
    if (GlobalData.shopcar_2.length) {
      this.setData({
        shopcard: GlobalData.shopcar_2
      })
      this.setCarEnable(this.price);
    }

  },
  // 卡种类
  onKindComplete: function () {
    that = this;
    this.setData({
      kindlist: GlobalData.KindList
    })
    // console.log("卡种类", this.data.kindlist);
    if (GlobalData.ProListID)
      dataManager.getProductList(1, 100, GlobalData.ProListID);
    else
      dataManager.getProductList(1, 100, this.data.kindlist[0].gameid);
  },
  onKindClick: function (e) {
    that = this
    var index = e.currentTarget.dataset.id;
    // console.log(index, e);
    var kindArr = that.data.kindlist;
    for (let i = 0; i < that.data.kindlist.length; i++) {
      kindArr[i]["color"] = '#999999';
      kindArr[i]["bgcolor"] = 'white';
      kindArr[i]["isClick"] = false;
    }
    kindArr[index]["color"] = 'black';
    kindArr[index]["bgcolor"] = '#ffbc7f';
    kindArr[index]["isClick"] = true;
    this.setData({
      kindlist: kindArr
    })
    dataManager.rigisterObserver("productlist", that.onProComplete, that);
    dataManager.getProductList(1, 100, that.data.kindlist[index]["gameid"]);
    // console.log(that.data.kindlist[index]["gameid"]);
  },
  onProComplete: function () {
    var prolistArr = GlobalData.ProductList[GlobalData.ProListID];
    //同步价格
    this.getPrice();
    if (GlobalData.shopcar_2.length)
      this.setCarEnable(this.getPrice());
    for (let i = 0; i < prolistArr.length; i++) {
      let start = prolistArr[i].ProductName.indexOf("房卡") + 2;
      if (start > 1)
        prolistArr[i].ProductName = prolistArr[i].ProductName.substring(start, prolistArr[i].ProductName.length);
      if (prolistArr[i].MemberPrice == prolistArr[i].ProductPrice) {
        prolistArr[i]["zhekou"] = false;
      } else
        prolistArr[i]["zhekou"] = true;

    }
    for (let i = 0; i < this.data.kindlist.length; i++) {
      for (let j = 0; j < prolistArr.length; j++) {
        // prolistArr[j]["ProductNumber"] = 0;
        if (this.data.kindlist[i].gameid == prolistArr[j].GameId) {
          prolistArr[j]["gamename"] = this.data.kindlist[i].title;
        }
      }
    }
    this.setData({
      prolist: prolistArr
    })
    //根据购物车同步数据
    this.refreshCount();
    // console.log("各种房卡", this.data.prolist);
  },
  // 减房卡数
  onReduce: function (e) {
    // console.log(e, "减");
    var card = e.currentTarget.dataset.id;
    for (let i = 0; i < GlobalData.shopcar_2.length; i++) {
      if (card.SysNo == GlobalData.shopcar_2[i].pro.SysNo) {
        if (GlobalData.shopcar_2[i].count == 1) {
          GlobalData.shopcar_2.splice(i, 1);
          break;
        }
        else if (GlobalData.shopcar_2[i].count) {
          GlobalData.shopcar_2[i].count--;
        }
      }
    }
    this.refreshCount();
    //价格
    this.setData({
      shopcard: GlobalData.shopcar_2,
    })
    var priceTemp = this.getPrice();
    if (priceTemp) {
      this.setData({
        price: "￥" + this.getPrice(),
      })
    } else {
      this.setData({
        price: 0,
      })
    }
    // console.log(this.data.price);
    if (this.data.price)
      this.setCarEnable(this.price);
    else
      this.setCarUnenable();
    if (this.getCount() == 0) {
      // this.renderer.setStyle(this.car.nativeElement, 'display', 'none');
      this.setData({
        isClick: false,
        ishide: true,
        height: "gouwuche",
        bind: "onClickShopCardup",
      })
    }

  },
  // 加房卡数
  onAdd: function (e) {
    //添加至购物车
    if (this.getCount() >= 99) {
      app.show("购买房卡数，最多不超过99张。");
      return;
    }

    var card = e.currentTarget.dataset.id;
    let sum = 0;
    console.log(card.SysNo)
    for (let i = 0; i < GlobalData.shopcar_2.length; i++) {
      if (card.SysNo == GlobalData.shopcar_2[i].pro.SysNo) {
        GlobalData.shopcar_2[i].count++;
        sum++;
        break;
      }
    }
    if (sum == 0) {
      GlobalData.shopcar_2.push({ pro: GlobalData.clone(card), count: 1 });
    }
    // console.log(GlobalData.shopcar_2);
    this.refreshCount();
    //价格
    this.setData({
      price: "￥" + this.getPrice(),
      shopcard: GlobalData.shopcar_2
    })
    if (this.data.price)
      this.setCarEnable(this.data.price);
    else
      this.setCarUnenable();
  },
  /**清空购物车 */
  onClearCard: function (e) {
    // console.log(e);
    GlobalData.shopcar_2 = [];
    this.setData({
      shopcard: GlobalData.shopcar_2,
      price: 0,
      isClick: false,
      height: "gouwuche",
      bind: "onClickShopCardup",
    })
    this.refreshCount();
    this.setCarUnenable();


  },
  /**根据购物车刷新产品数量 */
  refreshCount: function () {
    // console.log(GlobalData.shopcar_2, "购物车刷新产品数量");
    var arr = GlobalData.clone(this.data.prolist);
    for (let i = 0; i < arr.length; i++) {
      arr[i].ProductNumber = 0;
      for (let j = 0; j < GlobalData.shopcar_2.length; j++) {
        if (arr[i].SysNo == GlobalData.shopcar_2[j].pro.SysNo) {
          if (GlobalData.shopcar_2[j].count)
            arr[i].ProductNumber = GlobalData.shopcar_2[j].count;
          else
            arr[i].ProductNumber = 0;
          break;
        }
      }
    }
    this.setData({
      prolist: arr
    })

  },
  /**得到购物车总金额 */
  getPrice: function () {
    var pri = 0;
    for (let i = 0; i < GlobalData.shopcar_2.length; i++)
      pri += GlobalData.shopcar_2[i].pro.MemberPrice * GlobalData.shopcar_2[i].count;
    pri = Math.round(pri * 100) / 100;

    return pri;
  },
  getCount: function () {
    var procount = 0;
    for (let i = 0; i < GlobalData.shopcar_2.length; i++) {
      procount += GlobalData.shopcar_2[i].count;
    }
    this.setData({
      procount: procount
    })
    return procount;
  },
  /**设置购物车为可购买状态 */
  setCarEnable: function (price) {
    this.setData({
      procount: this.getCount(),
      pic: "Shopping2",
      price: "￥" + this.getPrice(),
    })

  },
  /**设置购物车为不可购买状态 */
  setCarUnenable: function () {
    this.setData({
      procount: 0,
      pic: "Shopping1",
      price: 0
    })

  },

  /**点击向上购物车 */
  onClickShopCardup: function (e) {
    if (this.data.procount == 0) {
      return;
    }
    // console.log("点击购物车", e);
    this.setData({
      bind: "onClickShopCarddown",
      height: "gouwuche2"
    })

  },
  /**点击向下购物车 */
  onClickShopCarddown: function (e) {
    // console.log("点击向下购物车", e);
    this.setData({
      height: "gouwuche3",
      bind: "onClickShopCardup",
    })

  },
  onshop: function (e) {
    // console.log("回到原点")
    this.setData({
      height: "gouwuche",
      bind: "onClickShopCardup",
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**跳转购买记录 */
  onBuyCardRecord: function () {
    this.setData({
      hidden: false
    })
    dataManager.OnRequestPost({ method: "getorderlist", "token": GlobalData.token, "ordertype": 2, "pageindex": GlobalData.ClubRechargeRecordother["index"], "pagesize": GlobalData.ClubRechargeRecordother["size"] }, this.onProcessClubRechargeRecord, false, this.onError);
  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  onProcessClubRechargeRecord: function (data) {
    for (let i = 0; i < data.length; i++) {
      GlobalData.ClubRechargeRecord[i] = GlobalData.clone(data[i]);
    }
    // console.log("clubrecord", dataManager.isClick("clubrecord"));
    // if (dataManager.isClick("clubrecord")) {
    //   return
    // } else {
    //   dataManager.btnObserver("clubrecord")
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../purchase/clubrecord/clubrecord', false);
    // }


  },

  /**跳转购买确认 */
  goBuyCardPay: function () {
    var cardNum = this.getCount()
    if (cardNum) {
      // if (dataManager.isClick("clubpay")) {
      //   return
      // } else {
      //   dataManager.btnObserver("clubpay")
      this.setData({
        hidden: false
      })
      dataManager.UseNavigateTo('../../purchase/clubpay/clubpay', false);
      // }
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})